package com.ticket.web.controller.yanchu;

import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.exception.base.BaseException;
import com.ticket.common.utils.StringUtils;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.system.domain.YanchuStatistics;
import com.ticket.system.domain.vo.MemberStatisticsVO;
import com.ticket.system.domain.vo.SettleGroupDetailVO;
import com.ticket.system.domain.vo.SettleGroupVO;
import com.ticket.system.service.ISysDeptService;
import com.ticket.system.service.IYanchuStatisticsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/system/statistics")
public class YanchuStatisticsController extends BaseController
{
    @Autowired
    private IYanchuStatisticsService yanchuStatisticsService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:statistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(YanchuStatistics yanchuStatistics)
    {
        startPage();
        List<YanchuStatistics> list = yanchuStatisticsService.selectYanchuStatisticsList(yanchuStatistics);
        return getDataTable(list);
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:statistics:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YanchuStatistics yanchuStatistics)
    {
        List<YanchuStatistics> list = yanchuStatisticsService.selectYanchuStatisticsList(yanchuStatistics);
        ExcelUtil<YanchuStatistics> util = new ExcelUtil<YanchuStatistics>(YanchuStatistics.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:statistics:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuStatisticsService.selectYanchuStatisticsById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:statistics:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YanchuStatistics yanchuStatistics)
    {
        return toAjax(yanchuStatisticsService.insertYanchuStatistics(yanchuStatistics));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:statistics:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YanchuStatistics yanchuStatistics)
    {
        return toAjax(yanchuStatisticsService.updateYanchuStatistics(yanchuStatistics));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:statistics:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yanchuStatisticsService.deleteYanchuStatisticsByIds(ids));
    }

    /**
     * 查询最近10天会员新增趋势
     */
    @GetMapping("/listUser/forAllUser")
    public List<Map<String,Object>> listUser()
    {
        List<Map<String,Object>> list = yanchuStatisticsService.selectStatisticsList();
        return list;
    }

    /**
     * 查询销售额统计
     */
    @GetMapping("/listStatics/forAllUser")
    public List<Map<String,Object>> listStatics(Integer type)
    {
        return yanchuStatisticsService.staticsOrder(type);
    }

    /**
     * 演出统计
     */
    @GetMapping("/performStatics/forAllUser")
    public List<Map<String,Object>> performStatics()
    {
        return yanchuStatisticsService.performStatics();
    }

    /**
     * 用户转化统计
     * @return
     */
    @GetMapping("/userTransStatics/forAllUser")
    public List<Map<String,Object>> userTransStatics()
    {
        return yanchuStatisticsService.userTransStatics();
    }

    /**
     * 统计数据初始化
     * @return
     */
    @GetMapping("/initPrevOrder/forAllUser")
    public AjaxResult initPrevOrder()
    {
        return success(yanchuStatisticsService.initPrevOrder());
    }

    @GetMapping("/initStatics/forAllUser")
    public AjaxResult initStatics(String date)
    {
        yanchuStatisticsService.staticsPrevOrder(date);
        return toAjax(1);
    }
    /**
     * 场次按演出汇总
     */
    @ApiOperation("演出汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionIds", value = "场次id,多个,号分隔", dataType = "String", dataTypeClass = String.class)
    })
//    @ApiResponses(value = {
//            @ApiResponse(code = 200, message = "订单详情",response = OrderVo.class)
//    })
    @GetMapping("/collectByPerform")
    public AjaxResult listSessionByPerform(@RequestParam String sessionIds)
    {
        List<Map<String,Object>> list = yanchuStatisticsService.listSessionByPerform(sessionIds);
        Map<String,Object> collectMap = yanchuStatisticsService.listSessionCollectByPerform(sessionIds);
        AjaxResult resultAjax = AjaxResult.success();
        resultAjax.put("list",list);
        resultAjax.put("collectMap",collectMap);
        return resultAjax;
    }

    @ApiOperation("管理端演出汇总导出")
//    @PreAuthorize("@ss.hasPermi('system:performStatistics:export')")
    @Log(title = "管理端演出汇总导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPerformStatistics")
    public void exportPerformStatistics(HttpServletResponse response, @RequestParam String sessionIds) {
        yanchuStatisticsService.exportPerformStatistics(response, sessionIds);
    }

    /**
     * 场次按演出汇总
     */
    @ApiOperation("场次汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionIds", value = "场次id,多个,号分隔", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("/collectBySession")
    public AjaxResult collectBySession(@RequestParam String sessionIds)
    {
        List<Map<String,Object>> list = yanchuStatisticsService.listSessionBySession(sessionIds);
        Map<String,Object> collectMap = yanchuStatisticsService.listSessionCollectBySession(sessionIds);
        AjaxResult resultAjax = AjaxResult.success();
        resultAjax.put("list",list);
        resultAjax.put("collectMap",collectMap);
        return resultAjax;
    }

    @ApiOperation("管理端场次汇总导出")
//    @PreAuthorize("@ss.hasPermi('system:performStatistics:export')")
    @Log(title = "管理端场次汇总导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSessionStatistics")
    public void exportSessionStatistics(HttpServletResponse response, @RequestParam String sessionIds) {
        yanchuStatisticsService.exportSessionStatistics(response, sessionIds);
    }

    /**
     * 场次按演出汇总
     */
    @ApiOperation("场次明细查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionIds", value = "场次id,多个,号分隔", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("/detailsBySession")
    public AjaxResult detailsBySession(@RequestParam String sessionIds)
    {
        List<Map<String,Object>> list = yanchuStatisticsService.listDetailsBySession(sessionIds);
        AjaxResult resultAjax = AjaxResult.success();
        resultAjax.put("list",list);
        return resultAjax;
    }

    @ApiOperation("场次明细查询导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sessionIds", value = "场次id,多个,号分隔", dataType = "String", dataTypeClass = String.class)
    })
//    @PreAuthorize("@ss.hasPermi('system:performStatistics:export')")
    @Log(title = "管理端场次汇总导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportListDetailsStatistics")
    public void exportListDetailsStatistics(HttpServletResponse response, @RequestParam String sessionIds) {
        yanchuStatisticsService.exportListDetailsStatistics(response, sessionIds);
    }


    @ApiOperation("院团结算汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "startTime", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "startTime", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/getGroupStatistics")
    public TableDataInfo<SettleGroupVO> getGroupStatistics(String startTime,String endTime,Long deptId) {
        if(StringUtils.isEmpty(startTime)||StringUtils.isEmpty(endTime)){
            throw new BaseException("开始时间或者结束时间不能为空！");
        }
        List<Long> childOperList = sysDeptService.getChildOperList(deptId);
        List<SettleGroupVO> settleGroupList = yanchuStatisticsService.getSettleGroupList(startTime, endTime,childOperList);
        return getDataTable(settleGroupList);
    }

    @ApiOperation("院团结算明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "startTime", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "startTime", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/getGroupDetailStatistics")
    public TableDataInfo<SettleGroupDetailVO> getGroupDetailStatistics(String startTime,String endTime,Long deptId) {
        if(StringUtils.isEmpty(startTime)||StringUtils.isEmpty(endTime)){
            throw new BaseException("开始时间或者结束时间不能为空！");
        }
        List<Long> childOperList = sysDeptService.getChildOperList(deptId);
        startPage();
        List<SettleGroupDetailVO> settleGroupDetail = yanchuStatisticsService.getSettleGroupDetail(startTime, endTime,childOperList);
        return getDataTable(settleGroupDetail);
    }


    @ApiOperation("院团结算汇总-导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "startTime", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "startTime", dataType = "String", dataTypeClass = String.class)
    })
//    @PreAuthorize("@ss.hasPermi('system:performStatistics:export')")
    @Log(title = "院团结算汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/exportGroupStatistics")
    public void exportGroupStatistics(HttpServletResponse response, String startTime,String endTime,String exportMsg,Long deptId) {
        if(StringUtils.isEmpty(startTime)||StringUtils.isEmpty(endTime)){
            throw new BaseException("开始时间或者结束时间不能为空！");
        }
        List<Long> childOperList = sysDeptService.getChildOperList(deptId);
        yanchuStatisticsService.exportSettleGroupList(response, startTime,endTime,exportMsg,childOperList);
    }

    @ApiOperation("院团结算明细-导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "startTime", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "startTime", dataType = "String", dataTypeClass = String.class)
    })
//    @PreAuthorize("@ss.hasPermi('system:performStatistics:export')")
    @Log(title = "团院结算明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportGroupDetailStatistics")
    public void exportGroupDetailStatistics(HttpServletResponse response, String startTime,String endTime,String exportMsg,Long deptId) {
        if(StringUtils.isEmpty(startTime)||StringUtils.isEmpty(endTime)){
            throw new BaseException("开始时间或者结束时间不能为空！");
        }
        List<Long> childOperList = sysDeptService.getChildOperList(deptId);
        yanchuStatisticsService.exportSettleGroupDetail(response, startTime,endTime,exportMsg,childOperList);
    }


    @ApiOperation("会员统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "startTime", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "startTime", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/getMemberStatistics")
    public TableDataInfo<MemberStatisticsVO> getMemberStatistics(String startTime,String endTime) {
        if(StringUtils.isEmpty(startTime)||StringUtils.isEmpty(endTime)){
            throw new BaseException("开始时间或者结束时间不能为空！");
        }
        startPage();
        List<MemberStatisticsVO> settleGroupList = yanchuStatisticsService.getMemberStatistics(startTime, endTime);
        return getDataTable(settleGroupList);
    }

    @ApiOperation("会员统计-导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "startTime", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "startTime", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/exportMemberStatistics")
    public void exportMemberStatistics(HttpServletResponse response, String startTime,String endTime,String exportMsg) {
        if(StringUtils.isEmpty(startTime)||StringUtils.isEmpty(endTime)){
            throw new BaseException("开始时间或者结束时间不能为空！");
        }
        yanchuStatisticsService.exportMemberStatistics(response, startTime,endTime,exportMsg);
    }

}
